package com.employ.job.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.employ.job.domain.EmpInterviews;
import com.employ.job.domain.EmpMessages;
import com.employ.job.service.IEmpInterviewsService;
import com.employ.job.service.IEmpMessagesService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.employ.common.annotation.Log;
import com.employ.common.core.controller.BaseController;
import com.employ.common.core.domain.AjaxResult;
import com.employ.common.enums.BusinessType;
import com.employ.job.domain.EmpApplications;
import com.employ.job.service.IEmpApplicationsService;
import com.employ.common.utils.poi.ExcelUtil;
import com.employ.common.core.page.TableDataInfo;

/**
 * 职位申请Controller
 * 
 * @author ruoyi
 * @date 2025-02-24
 */
@RestController
@RequestMapping("/job/applications")
public class EmpApplicationsController extends BaseController
{
    @Autowired
    private IEmpApplicationsService empApplicationsService;

    @Autowired
    private IEmpInterviewsService empInterviewsService;

    @Autowired
    private IEmpMessagesService empMessagesService;

    /**
     * 查询职位申请列表
     */
    @PreAuthorize("@ss.hasPermi('job:applications:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmpApplications empApplications)
    {
        startPage();
        List<EmpApplications> list = empApplicationsService.selectEmpApplicationsList(empApplications);
        return getDataTable(list);
    }

    /**
     * 导出职位申请列表
     */
    @PreAuthorize("@ss.hasPermi('job:applications:export')")
    @Log(title = "职位申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmpApplications empApplications)
    {
        List<EmpApplications> list = empApplicationsService.selectEmpApplicationsList(empApplications);
        ExcelUtil<EmpApplications> util = new ExcelUtil<EmpApplications>(EmpApplications.class);
        util.exportExcel(response, list, "职位申请数据");
    }

    /**
     * 获取职位申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('job:applications:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(empApplicationsService.selectEmpApplicationsById(id));
    }

    /**
     * 新增职位申请
     */
    @PreAuthorize("@ss.hasPermi('job:applications:add')")
    @Log(title = "职位申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmpApplications empApplications)
    {
        return toAjax(empApplicationsService.insertEmpApplications(empApplications));
    }

    /**
     * 修改职位申请
     */
    @PreAuthorize("@ss.hasPermi('job:applications:edit')")
    @Log(title = "职位申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmpApplications empApplications)
    {
        int i = empApplicationsService.updateEmpApplications(empApplications);
        if (i>0){
            //写入面试安排表
            EmpInterviews empInterviews = new EmpInterviews();
            empInterviews.setApplicationId(empApplications.getId());
            empInterviews.setStatus("1");
            empInterviewsService.insertEmpInterviews(empInterviews);
            //写入站内信通知
            EmpMessages empMessages = new EmpMessages();
            empMessages.setSenderId(this.getUserId());
            empMessages.setReceiverId(this.getUserId());
            empMessages.setSubject("邀请面试");
            empMessagesService.insertEmpMessages(empMessages);

            return success("预约成功");
        }

        return error("预约失败");
    }

    /**
     * 删除职位申请
     */
    @PreAuthorize("@ss.hasPermi('job:applications:remove')")
    @Log(title = "职位申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(empApplicationsService.deleteEmpApplicationsByIds(ids));
    }
}
